package com.yztc.youbadbad.ui.other.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import com.yztc.youbadbad.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;

public class LogInActivity extends AppCompatActivity {
    //Tab标签常量
    private static final String[] TAB_TITLES = {"快速登录", "密码登录"};

    private List<Fragment> mainFragments;
    private MainAdapter mainAdapter;
    private ViewPager mainViewPager;
    private Context mContext;
    private TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_in);

        ButterKnife.bind(this);

        //初始化Fragment
        initData();
        //初始化视图
        initView();
        //初始化tab
        initTab();
    }


    private void initTab() {
        tabLayout = (TabLayout) findViewById(R.id.layout_tab);
        //将TabLyout关联到ViewPager
        tabLayout.setupWithViewPager(mainViewPager);
    }

    private void initView() {
        mainAdapter = new MainAdapter(getSupportFragmentManager());
        mainViewPager = (ViewPager) findViewById(R.id.vp_main);
        mainViewPager.setAdapter(mainAdapter);

    }

    //初始化Fragment
    private void initData() {
        mainFragments = new ArrayList<>();
        LoginFragment fragment1 = new LoginFragment();
        QuickFragment fragment2 = new QuickFragment();
        mainFragments.add(fragment1);
        mainFragments.add(fragment2);

    }

    @OnClick(R.id.imageview_back)
    public void onClick() {
        finish();
    }


    class MainAdapter extends FragmentPagerAdapter {

        public MainAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mainFragments.get(position);
        }

        @Override
        public int getCount() {
            return mainFragments.size();
        }

        //设置tab按钮的标题
        @Override
        public CharSequence getPageTitle(int position) {
            return TAB_TITLES[position];
        }
    }
}
